package com.uit.project.bustrip;

import java.util.List;

import com.uit.project.bustrip.appconfig.AppsConfig;
import com.uit.project.bustrip.bo.CustomerBOImpl;
import com.uit.project.bustrip.bointerface.CustomerBO;
import com.uit.project.bustrip.entity.Customer;
import com.uit.project.bustrip.valueobject.LoginInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CustomerLogin extends Activity {

	private EditText phone;
	private EditText password;
	private Button btnLogin;
	private Button btnRegister;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		checkLogin();
	}
	private void checkLogin(){
		CustomerBO customerBO = new CustomerBOImpl(getApplicationContext());
        List<Customer> customers = customerBO.findAll();
        if(customers.size()>0){
        	Intent i = new Intent(getApplicationContext(),Home.class);
			startActivity(i);
			finish();
        }
        else{
    		setContentView(R.layout.activity_customer_login);
    		initView();
        }
	}
	private void initView(){
		phone = (EditText) findViewById(R.id.phone);
		password = (EditText) findViewById(R.id.password);
		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnRegister = (Button) findViewById(R.id.btnRegister);
		final CustomerBOImpl custBO = new CustomerBOImpl(this); 
		btnLogin.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
				StrictMode.setThreadPolicy(policy);
				String txtPhone = phone.getText().toString();
				String txtPass = password.getText().toString();
				LoginInfo loginInfo = custBO.loginAsync(txtPhone, txtPass); 
				affterLoginSuccess(loginInfo);
			}
		});
		btnRegister.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(getApplicationContext(),Register.class);
				startActivity(i);
			}
		});
	}
	private void affterLoginSuccess(LoginInfo loginInfo){
		if(loginInfo == null){
			Toast.makeText(getApplicationContext(), AppsConfig.MESSAGE_CONNECTION_ERROR, Toast.LENGTH_SHORT).show();
			return;
		}
		if(loginInfo.getResult().equals(AppsConfig.RESULT_SUCCESS)){
//			Toast.makeText(getApplicationContext(), loginInfo.getMessage(), Toast.LENGTH_SHORT).show();
			Customer customer = loginInfo.getData();
			CustomerBO customerBO = new CustomerBOImpl(getApplicationContext());
			long rs = customerBO.insert(customer);
			Intent i = new Intent(getApplicationContext(),Home.class);
			startActivity(i);
		}
		else{
			Toast.makeText(getApplicationContext(), loginInfo.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.customer_login, menu);
		return true;
	}

}
